//
//  Extension.swift
//  demo
//
//  Created by toro宇 on 2020/2/13.
//  Copyright © 2020 CodeYu. All rights reserved.
//

import UIKit
import SnapKit

extension UIButton{
    
    class func setJumpButton(_ target:Any?,antion:Selector){
        let btn = UIButton(type: .custom)
        btn.backgroundColor = .gray
        btn.setTitle("跳转", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        btn.addTarget(target, action: antion, for: .touchUpInside)
        if let _target = target as? UIViewController{
            _target.view.addSubview(btn)
            btn.snp.makeConstraints({(make) in
                make.width.equalTo(100.0)
                make.height.equalTo(50.0)
                make.center.equalToSuperview()
                
            })
        }
        
    }
}


/*
 
 //use example:
 
 let color = UIColor.red
 
 let hex = color.toHexString
 // hex == "FF0000"
 
 let color = UIColor(hex: "ff0000")
 //color == UIColor.red
 
 */

extension UIColor{
    var toHexString: String {
        var r: CGFloat = 0
        var g: CGFloat = 0
        var b: CGFloat = 0
        var a: CGFloat = 0
        
        self.getRed(&r, green: &g, blue: &b, alpha: &a)
        
        return String(
            format: "%02X%02X%02X",
            Int(r * 0xff),
            Int(g * 0xff),
            Int(b * 0xff)
        )
    }
    
    convenience init(hex: String) {
        let scanner = Scanner(string: hex)
        scanner.scanLocation = 0
        
        var rgbValue: UInt64 = 0
        
        scanner.scanHexInt64(&rgbValue)
        
        let r = (rgbValue & 0xff0000) >> 16
        let g = (rgbValue & 0xff00) >> 8
        let b = rgbValue & 0xff
        
        self.init(
            red: CGFloat(r) / 0xff,
            green: CGFloat(g) / 0xff,
            blue: CGFloat(b) / 0xff, alpha: 1
        )
    }
}



